// A-S-M team
// Project: BattleCity
// Created: 23.03.2011
// Last change: 24.03.2011
// Description: implementation class-singleton "Painter" draws all objects of game

#include "painter.h"
#include "resources.h"
#include "consts.h"
#include "play.h"

Painter * Painter::p_instance = NULL;

void Painter::clear()
{
	_gc.clear();
}

void Painter::drawGrid()
{
	for(int i=0;i<=ORIGINAL_SCREEN_WIDTH+ORIGINAL_MIN_PIXEL;i+=ORIGINAL_MIN_PIXEL){
		CL_Draw::line(_gc,i*_multiplier,0,i*_multiplier,ORIGINAL_SCREEN_HEIGHT*_multiplier,CL_Colorf::red);
		CL_Draw::line(_gc,0,i*_multiplier,ORIGINAL_SCREEN_WIDTH*_multiplier,i*_multiplier,CL_Colorf::red);
	}

	for(int i=0;i<WIDTH_CELL_COUNT;i++){
            for(int j=0;j<HEIGHT_CELL_COUNT;j++){
                    if(Play::getInstance()->getFieldItem(i, j)!=NULL)
                            CL_Draw::fill(_gc,i*ORIGINAL_MIN_PIXEL*_multiplier,j*ORIGINAL_MIN_PIXEL*_multiplier,(i+1)*ORIGINAL_MIN_PIXEL*_multiplier,(j+1)*ORIGINAL_MIN_PIXEL*_multiplier,CL_Colorf::blue);
			}
	}

}

void Painter::drawPoint(float x, float y, float size)
{
	CL_Draw::circle(_gc, x*_multiplier, y*_multiplier, size*_multiplier, CL_Colorf::blue);
}

void Painter::drawText(float x, float y, std::string text)
{
	CL_Font font(_gc, "Tahoma", (int)(7*_multiplier));
	font.draw_text(_gc, x*_multiplier, y*_multiplier, text, CL_Colorf::white);
}

void Painter::drawSprite(float x, float y, CL_Sprite* sprite){
	//drawSpritePart(x, y, sprite, CL_Rectf(0, 0, sprite->get_width(), sprite->get_height()));
	sprite->draw(_gc, CL_Rectf(x * _multiplier, y * _multiplier, (x+sprite->get_width()) * _multiplier, (y+sprite->get_height()) * _multiplier));
}


void Painter::drawSpritePart(float x, float y, CL_Sprite* sprite, CL_Rectf src){
	sprite->draw(_gc, src, CL_Rectf(x*_multiplier, y*_multiplier, (x + src.get_width())*_multiplier, (y + src.get_height())*_multiplier));
}

CL_Sprite* Painter::loadSprite(CL_String sprite_name){
	return new CL_Sprite(_gc, sprite_name, &Resources::getInstance()->getManager());
}